<?php
namespace app\admin\controller;
use think\Db;

class Carousel extends SessionCheck
{
    public function index()
    {
		$aid = input('get.aid');
		$where = [];
		if(!empty($aid)){
			$where['aid'] = $aid;
		}
		$pages = pages('carousel',$where,input('get.page'));
		$data = Db::table("carousel")->whereIn('aid',session('aid'))->where($where)->limit($pages['limit'])->order('id desc')->select();
        return view('',['data'  => $data,'pages' => $pages['pages'],'menu_list'=>$this->menu_list]);
    }
	
	public function edit()
    {
		$id = (int)request()->get('id');
		if(request()->isPost()){
			$data = request()->post();
			if(!$data['aid']){
			    echo '请选择城市';
            }else{
			    $data['activity_id'] = time().rand(111,999);
                if($id){
                    Db::table("carousel")->where(['id' => $id])->update($data);
                }else{
                    $id = Db::table("carousel")->insertGetId($data);
                }
            }
		}
        $data = Db::table("carousel")->where(['id' => $id])->find();
        return view('',['data'  => $data,'menu_list'=>$this->menu_list]);
    }
	
	public function del()
    {
		$id = input("get.id")?input("get.id"):0;
		$page = input("get.page");
		Db::table("carousel")->whereIn('aid',session('aid'))->delete($id);
		$this->redirect(url('/admin/carousel').'?page='.$page);
		exit;
    }
}
